package com.hc.sort.call;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.*;
import javax.swing.border.*;

import com.hc.sort.server.pojo.BusinessWindow;
import com.hc.sort.server.pojo.Caller;
import com.hc.sort.server.service.BusinessWindowService;
import com.hc.sort.server.service.BusinessWindowServiceImpl;
import com.hc.sort.server.service.CallerService;
import com.hc.sort.server.service.CallerServiceImpl;

public class LoginUI extends JFrame {
	//依赖的服务
	BusinessWindowService busWinService=new BusinessWindowServiceImpl();
	CallerService cService=new CallerServiceImpl();

	//业务类型列表
	List<BusinessWindow> busWinList;
	
	private JPanel contentPane;
	private JTextField textNo;
	private JTextField textPassword;
	JComboBox comboBoxWindow;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginUI frame = new LoginUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginUI() {
		setTitle("员工登录窗口");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 519, 421);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblWindow = new JLabel("窗口：");
		lblWindow.setFont(new Font("宋体", Font.PLAIN, 16));
		lblWindow.setBounds(89, 47, 54, 29);
		contentPane.add(lblWindow);
		
		JLabel lblNO = new JLabel("工号：");
		lblNO.setFont(new Font("宋体", Font.PLAIN, 16));
		lblNO.setBounds(89, 136, 54, 29);
		contentPane.add(lblNO);
		
		JLabel lblPassword = new JLabel("密码：");
		lblPassword.setFont(new Font("宋体", Font.PLAIN, 16));
		lblPassword.setBounds(89, 224, 54, 37);
		contentPane.add(lblPassword);
		
		textNo = new JTextField();
		textNo.setColumns(10);
		textNo.setBounds(153, 135, 228, 34);
		contentPane.add(textNo);
		
		textPassword = new JTextField();
		textPassword.setColumns(10);
		textPassword.setBounds(153, 227, 228, 34);
		contentPane.add(textPassword);
		
		JButton LoginBtn = new JButton("登录");
		LoginBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// 调用业务方法验证登录
				String workNo=textNo.getText();
				String password=textPassword.getText();
				
				Caller caller;//登录用户
				if ("".equals(workNo.trim()) || "".equals(password.trim())) {
					JOptionPane.showMessageDialog(null, "请输入用户名密码！");
					return;
				}else {
					caller=cService.findByWorknoAndPwd(workNo, password);
					if (caller == null) {
						JOptionPane.showMessageDialog(null, "登陆失败，用户名或密码错误!");
						return;
					}
				}
				
				//获取登录的窗口
				String winNo=comboBoxWindow.getSelectedItem().toString();
				BusinessWindow busWin=null;
				for (int i = 0; i < busWinList.size(); i++) {
					if (busWinList.get(i).getNo().equals(winNo)) {
						busWin=busWinList.get(i);
					}
				}
				System.out.println(busWin);
				JOptionPane.showMessageDialog(null, "登录成功！");
				CallUI call=new CallUI(busWin,caller);
				call.setVisible(true);
				
				LoginUI.this.setVisible(false);
			}
		});
		LoginBtn.setFont(new Font("宋体", Font.PLAIN, 16));
		LoginBtn.setBounds(204, 299, 114, 37);
		contentPane.add(LoginBtn);
		
		comboBoxWindow = new JComboBox();
		comboBoxWindow.setBounds(153, 48, 228, 29);
		contentPane.add(comboBoxWindow);
		
		//初始化下拉框
		init();
	}
	
	void init() {
		//初始化
		busWinList=busWinService.findAll();
		//列出来
		String[] comboxData=new String[busWinList.size()];
		for (int i = 0; i < busWinList.size(); i++) {
			comboxData[i]=busWinList.get(i).getNo();
		}
		comboBoxWindow.setModel(new DefaultComboBoxModel(comboxData));
	}
}
